<?php

namespace app\payment;

use app\services\ConfigService;
use support\Paypal;
use Yansongda\Pay\Pay;
use function EasyWeChat\Kernel\Support\get_client_ip;

class PaypalPay extends AbstractPay
{
    public function pay()
    {
        $config = ConfigService::getByGroup('paypal');

        $pay = new Paypal(
            $config['paypal_client_id'],
            $config['paypal_client_secret'],
            true
        );

        $pay->setReturnUrl(getenv('PC_URL'));
        $pay->setCancelUrl(getenv('PC_URL'));

        return $pay->createOrder($this->data['total_amount'], $this->data['order_no']);
    }

    public function payBack()
    {
        Pay::config(ConfigService::getPaymentConfig(PayFactory::WECHAT));

        return Pay::wechat()->refund([
            'out_trade_no' => $this->data['order_no'],
            'out_refund_no' => $this->data['order_no'] . '_refund',
            'amount' => [
                'total' => $this->data['total_amount'] * 100,
                'refund' => $this->data['total_amount'] * 100,
                'currency' => 'CNY'
            ]
        ]);
    }
}